home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Scan / 32-bit_x86 / Francais / cpsimage.cab / data / xipProcs / scanCSDKtoXIPXML.proc < prev    next >
Text File  |  2009-03-16  |  9KB  |  299 lines

  1. // $Id: scanCSDKtoXIPXML.elf,v 1.13 2008/07/12 17:49:14 mfrey Exp $ 
  2.  
  3. #load "sys/xipxml.elf";
  4.  
  5. GLOBAL INTEGER fs;
  6.  
  7. private
  8. PROCEDURE getCSDKPage( XmlNode nd, INTEGER pg)
  9.   RETURNS (XmlNode pageNode)
  10. {
  11.     INTEGER counter = 1; 
  12.     XmlNode c;
  13.     STRING word = "page";
  14.     
  15.     for( c = nd.getFirstChild(); c ; c = c.getNextSibling() ) {     
  16.        if (c.getNodeName() == word) {
  17.          if( counter == pg ){ 
  18.         break ; 
  19.          }
  20.          counter++;     
  21.  
  22.     }
  23.    } 
  24.    pageNode = c; 
  25. }
  26.  
  27. private 
  28. PROCEDURE getCSDKPageStr(XmlNode nd, DOUBLE yres, DOUBLE xres)
  29.   RETURNS(STRING pageStr)
  30. {
  31.     STRING str; 
  32.     XmlAttr at; 
  33.     INTEGER i,j,i1 = 0;
  34.  
  35.  
  36.     if (nd.getNodeName() == "page") {
  37.         str =  "<" + nd.getNodeName();
  38.  
  39.         if( nd.hasAttributes() ) {
  40.             XmlNamedNodeMap map = nd.getAttributes();
  41.  
  42.             for( i = map.getLength(); i != 0; i-- ) {
  43.                 at = map.item( index:i-1 );
  44.                 if (at.getName() == "x-res") {
  45.                 str = str + " " + "res" + "=\"" + at.getValue() + "\"";
  46.                 str = str + " " + "x0" + "=\"" + "0\"";
  47.                 str = str + " " + "y0" + "=\"" + "0\"";
  48.                 }
  49.                 else if (at.getName() == "width")
  50.                 {
  51.                    i1 = (at.getValue() * xres) / 1440.0 ;
  52.                    str = str + " " + "xw" + "=\"" + i1 + "\"";
  53.                }
  54.                else if (at.getName() == "height")
  55.                {
  56.                    i1 = (at.getValue() * yres) / 1440.0 ;
  57.                    str = str + " " + "yh" + "=\"" + i1 + "\"";
  58.                }
  59.            }
  60.            str = str + " " + "ocrCharCount" + "=\"" + "0\"";
  61.            str = str + " " + "ocrQCharCount" + "=\"" + "0\"";
  62.            str = str + " " + "ocrECharCount" + "=\"" + "0\"";
  63.            str = str + " " + "ocrWordCount" + "=\"" + "0\"";
  64.         }
  65.         str = str + ">\n";
  66.     } 
  67.     pageStr = str ; 
  68. }
  69.  
  70. private
  71. PROCEDURE getCSDKBaseline( XmlNode nd, DOUBLE yres)
  72.     RETURNS(INTEGER baseline)
  73. {
  74.     INTEGER i;
  75.     XmlAttr at;
  76.  
  77.     if( nd.hasAttributes() ) {
  78.             XmlNamedNodeMap map = nd.getAttributes();
  79.  
  80.             for( i = map.getLength(); i != 0; i-- ) {
  81.                 at = map.item( index:i-1 );
  82.  
  83.         if ( at.getName() == "baseline") {
  84.         baseline = (at.getValue() * yres) / 1440.0 ;
  85.         }
  86.             }
  87.     }
  88. }
  89.  
  90. private
  91. PROCEDURE getCSDKFontSize( XmlNode nd)
  92. {
  93.     INTEGER i;
  94.     XmlAttr at;
  95.  
  96.     if( nd.hasAttributes() ) {
  97.             XmlNamedNodeMap map = nd.getAttributes();
  98.  
  99.             for( i = map.getLength(); i != 0; i-- ) {
  100.                 at = map.item( index:i-1 );
  101.  
  102.         if ( at.getName() == "fs") {
  103.         fs = at.getValue() ;
  104.         }
  105.             }
  106.     }
  107. }
  108.  
  109. private
  110. PROCEDURE getCSDKyh (INTEGER fs, INTEGER yin)
  111.   RETURNS (INTEGER ytmp)
  112. {
  113.     ytmp = yin;
  114.  
  115.     if ( fs <= 900)                             ytmp = 36;
  116.     else if ( (fs >= 901)  && (fs <= 1000) )    ytmp = 41;
  117.     else if ( (fs >= 1001) && (fs <= 1200) )    ytmp = 48;
  118.     else if ( (fs >= 1201) && (fs <= 1400) )    ytmp = 60;
  119.     else if ( (fs >= 1401) && (fs <= 1500) )    ytmp = 63;
  120.     else if ( (fs >= 1501) && (fs <= 1700) )    ytmp = 68;
  121.     else if ( (fs >= 1701) && (fs <= 1900) )    ytmp = 79;
  122.     else if ( (fs >= 1901) && (fs <= 2100) )    ytmp = 86;
  123.     else if ( (fs >= 2101) && (fs <= 2200) )    ytmp = 94;
  124.     else if ( (fs >= 2201) && (fs <= 2400) )    ytmp = 100;
  125.     else if ( (fs >= 2401) && (fs <= 2600) )    ytmp = 110;
  126.     else if ( (fs >= 2601) && (fs <= 2800) )    ytmp = 117;
  127.     else if ( (fs >= 2801) && (fs <= 3000) )    ytmp = 128;
  128.     else if ( (fs >= 3001) && (fs <= 3400) )    ytmp = 140;
  129.     else if ( (fs >= 3401) && (fs <= 3800) )    ytmp = 156;
  130.     else if ( (fs >= 3801) && (fs <= 4400) )    ytmp = 177;
  131.     else if ( (fs >= 4401) && (fs <= 5600) )    ytmp = 215;
  132.     else if ( (fs >= 5601) && (fs <= 6700) )    ytmp = 265;
  133.     else if ( (fs >= 6701) && (fs <= 6900) )    ytmp = 280;
  134.     else if ( (fs >= 6901) && (fs <= 8200) )    ytmp = 310;
  135.     else if ( (fs >= 8201) && (fs <= 9000) )    ytmp = 350;
  136.  
  137. }
  138.  
  139. /* @CSDKtoXIPXML
  140.   //DESCRIPTION
  141.   Create a text string for a XML text layer from an XML Node. The XML Node
  142.   is created from CSDK OCR data.
  143. */
  144. private
  145. PROCEDURE CSDKtoXIPXML( XmlNode nd, DOUBLE d1, DOUBLE d2 , INTEGER b1)
  146.     RETURNS(STRING wdStr)
  147. {
  148.     INTEGER i, j;
  149.     INTEGER x0, x1, y0, y1;
  150.     XmlNode c, mynode;
  151.     XmlAttr at;
  152.     STRING  str;
  153.     STRING word = "wd";   
  154.     STRING ln = "ln";
  155.     INTEGER i1 = 0;
  156.     INTEGER baseline;
  157.     INTEGER ytmp;
  158.     DOUBLE xres, yres;
  159.     STRING rmChar;  
  160.     INTEGER index;
  161.    
  162.     yres = d1;
  163.     xres = d2;
  164.     baseline = b1;
  165.     // If it is an Element get the attributes
  166.     if( (nd.getNodeType() == XmlNode.XML_ELEMENT_NODE ) ) {
  167.    
  168.     // Get baseline
  169.     if (nd.getNodeName() == ln)
  170.     {
  171.         baseline= getCSDKBaseline(nd:nd, yres:yres);
  172.         getCSDKFontSize(nd:nd);        
  173.     }
  174.  
  175.        // Get each word
  176.      else if (nd.getNodeName() == word) {
  177.  
  178.         str = "<Glyphs ";
  179.     str = str + "famStyle= \"" + "0\"" ;
  180.     str = str + " isoCSet= \"" + "1\"" ;
  181.  
  182.         if( nd.hasAttributes() ) {
  183.             XmlNamedNodeMap map = nd.getAttributes();
  184.  
  185.             for( i = map.getLength(); i != 0; i-- ) {
  186.                 at = map.item( index:i-1 );
  187.  
  188.         if ( at.getName() == "l") {
  189.         i1 = (at.getValue() * xres) / 1440.0 ;
  190.         str = str + " x0 " + "=\"" + i1  + "\"";
  191.         x0  = i1;
  192.         }
  193.         else if (at.getName() == "t") {
  194.         i1 = (at.getValue() * yres) / 1440.0 ;
  195.         y0 = i1;
  196.         }
  197.         else if (at.getName() == "r") {
  198.         i1 = (at.getValue() * xres) / 1440.0 ;
  199.         x1 = i1;
  200.         }
  201.         else if (at.getName() == "b") {
  202.         i1 = (at.getValue() * yres) / 1440.0 ;
  203.         str = str + " yb " + "=\"" + baseline + "\"";
  204.         y1 = i1;
  205.         }    
  206.             }
  207.         str = str + " " + "xw=\"" + (x1 - x0) + "\"";
  208.         ytmp = getCSDKyh(fs:fs, yin: y1-y0 );
  209.         str = str + " " + "yh=\"" + ytmp + "\"";
  210.         }
  211.     if ( nd.hasChildren()){
  212.            mynode = nd.getFirstChild();
  213.     
  214.        /*If the nodes value had quotes replace with " (html quote) */ 
  215.           rmChar =  mynode.getNodeValue();
  216.  
  217.        /*Looks like & is invalid too. Eventually we need to find out all the
  218.        * invalid char and remove them 
  219.        */     
  220.        index = rmChar.indexOf(match:"&") ;
  221.        while( index > -1 ){
  222.           rmChar = rmChar.replace(sub:"&", run: 1, start: index);
  223.           index =    rmChar.indexOf(match:"&", start: index+5) ;
  224.            }
  225.        index = rmChar.indexOf(match:"\"") ; 
  226.        while( index > -1 ){
  227.           rmChar = rmChar.replace(sub:""", run: 1, start: index);
  228.           index =    rmChar.indexOf(match:"\"") ;
  229.            }
  230.        str = str + " str=\"" + rmChar + "\"" + ">\n</Glyphs>"; 
  231.         }else{
  232.        rmChar = " "; 
  233.        str = str + " str=\"" + rmChar + "\"" + ">\n</Glyphs>"; 
  234.         }
  235.  
  236.     } 
  237.  
  238.     for( c = nd.getFirstChild(); c ; c = c.getNextSibling() ) {     
  239.         str = str + CSDKtoXIPXML( nd:c, d1:d1 , d2:d2,  b1:baseline);
  240.     }
  241.   } 
  242.   wdStr = str;
  243. }
  244.  
  245.  
  246. /* @scanCSDKtoXIPXML
  247.   //DESCRIPTION
  248.   Create a XIPIMAGE with XIPXML Text layer from CSDK data.  The CSDK data is assumed to come
  249.   from a file (filename).  You can also request a page number to process and set alternative
  250.   resolutions.
  251. */
  252. PROCEDURE scanCSDKtoXIPXML ( STRING filename, INTEGER pgnumber, DOUBLE xres, DOUBLE yres)
  253.   RETURNS (XIPIMAGE output)
  254. {
  255.     STRING pageStr, wdStr;
  256.     XmlDocumentBuilder db;
  257.     INTEGER  b1;
  258.     XmlNode page; 
  259.     
  260.     // Read input file
  261.     try
  262.         { XmlDocument d = db.parseFile(filename: filename); }
  263.     catch
  264.         { print " Error: " + status.message; end; }    
  265.  
  266.     // Extract XML elements
  267.  
  268.     if( d ) {
  269.     
  270.         XmlNode root = d.getDocumentElement();
  271.  
  272.         // Get page element data
  273.  
  274.         page =  getCSDKPage( nd:root,  pg: pgnumber);
  275.  
  276.         pageStr =  getCSDKPageStr( nd:page, yres:yres, xres:xres);
  277.  
  278.         // get selected elements
  279.  
  280.         wdStr = CSDKtoXIPXML( nd:page, d1:yres, d2:xres, b1:b1);
  281.         
  282.     pageStr = pageStr + wdStr + "</" + "page" + ">"; 
  283.  
  284.     }
  285.     else
  286.     {
  287.          print "Failed to read XML file";
  288.     }
  289.  
  290.     // Write text/xml to XIPLayer
  291.  
  292.     XIPLAYER layer = new (XIPLAYER, layerType: XIP_Text, visibility: FALSE);
  293.     output = output.addLayer (image: layer, ltype: XIP_Text);
  294.     output.setMember (num:0, member: "text", value: pageStr);
  295.  
  296.     // Return XIPImage
  297. }    
  298.  
  299.